package com.first.config.druid;

import java.sql.SQLException;
import java.util.Properties;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.alibaba.druid.pool.DruidDataSource;

@Configuration
/*@EnableConfigurationProperties(DruidProperties.class)
@ConditionalOnClass(DruidDataSource.class)
@ConditionalOnProperty(prefix = "spring.druid.datasource", name = "url")
@AutoConfigureBefore(DataSourceAutoConfiguration.class)*/
public class DruidConfiguration {
    
    @Autowired
    private DruidProperties properties;
    
    @Bean
    public DataSource dataSource()  {
    	  DruidDataSource dataSource = new DruidDataSource();
          dataSource.setDriverClassName(properties.getDriverClassName());
          dataSource.setUrl(properties.getUrl());
          dataSource.setUsername(properties.getUsername());
          dataSource.setPassword(properties.getPassword());
          dataSource.setInitialSize(properties.getInitialSize());
          dataSource.setMinIdle(properties.getMinIdle());
          dataSource.setMaxActive(properties.getMaxActive());
          dataSource.setMaxWait(properties.getMaxWait());
          dataSource.setTimeBetweenEvictionRunsMillis(properties.getTimeBetweenEvictionRunsMillis());
          dataSource.setMinEvictableIdleTimeMillis(properties.getMinEvictableIdleTimeMillis());
          String validationQuery = properties.getValidationQuery();
          if (validationQuery != null && !"".equals(validationQuery)) {
              dataSource.setValidationQuery(validationQuery);
          }
          dataSource.setTestWhileIdle(properties.isTestWhileIdle());
          dataSource.setTestOnBorrow(properties.isTestOnBorrow());
          dataSource.setTestOnReturn(properties.isTestOnReturn());
          if(properties.isPoolPreparedStatements()){
              dataSource.setMaxPoolPreparedStatementPerConnectionSize(properties.getMaxPoolPreparedStatementPerConnectionSize());
          }
          try {
			dataSource.setFilters(properties.getFilters());
		} catch (SQLException e) {
			e.printStackTrace();
		}//这是最关键的,否则SQL监控无法生效
          String connectionPropertiesStr = properties.getConnectionProperties();
          if(connectionPropertiesStr != null && !"".equals(connectionPropertiesStr)){
              Properties connectProperties = new Properties();
              String[] propertiesList = connectionPropertiesStr.split(";");
              for(String propertiesTmp:propertiesList){
                  String[] obj = propertiesTmp.split("=");
                  String key = obj[0];
                  String value = obj[1];
                  connectProperties.put(key,value);
              }
              dataSource.setConnectProperties(connectProperties);
          }
          dataSource.setUseGlobalDataSourceStat(properties.isUseGlobalDataSourceStat());

          return dataSource;
    }
}
